﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Xml.Linq;
using System.IO.IsolatedStorage;

namespace NotificationReceiver
{
    public  class Exceptions
    {
       

        public static void SaveException1(string message, string applicationName, string username)
        {
            string filepath = string.Empty;
            using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                XDocument doc;
                IsolatedStorageFileStream isoStream;
                if(!isoStore.DirectoryExists("exceptions"))
                    isoStore.CreateDirectory("exceptions");               
                filepath = "exceptions/" + username + ".xml";
                //if the records file does not exist 
                if (!isoStore.FileExists(filepath))
                {

                    int id = 1;
                    //create new document 
                    isoStream = new IsolatedStorageFileStream(filepath, FileMode.Create, isoStore);
                    doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
                                new XElement("exceptions",
                                    new XElement("exception",
                                        new XElement("message", message),
                                           new XElement("applicationname", applicationName),
                                            new XElement("date", System.DateTime.Now.ToString()))));
                    doc.Save(isoStream);
                    isoStream.Close();
                }
                else
                {
                    isoStream = new IsolatedStorageFileStream(filepath, FileMode.Open, FileAccess.Read, isoStore);
                    doc = XDocument.Load(isoStream);
                    isoStream.Close();
                    doc.Root.Add(new XElement("exception",
                                        new XElement("message", message),
                                           new XElement("applicationname", applicationName),
                                            new XElement("date", System.DateTime.Now.ToString())));
                    isoStream = new IsolatedStorageFileStream(filepath, FileMode.Create, FileAccess.Write, isoStore);
                    doc.Save(isoStream);
                    isoStream.Close();
                }
            }
        }

        public static void SaveException(string message, string applicationName, string username)
        {
            NotificationService.Service1Client client = new NotificationService.Service1Client();
            client.SaveExceptiontoCloudAsync(message, "NotificationReceiver", username);
            client.SaveExceptiontoCloudCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_SaveExceptiontoCloudCompleted);
        }

        static void client_SaveExceptiontoCloudCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            
        }

    }
}
